/**
 * 
 */
package com.roc.udptest;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author dxpeng
 *
 */
public class UdpServer implements Runnable {
	DatagramSocket ds=null;
	DatagramPacket dp=null;
	@Override
	public void run() {
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		byte [] buf=new byte[1024]; 
		while(true){
		 try {
			if(ds==null){
				ds=new DatagramSocket(8088);
				//ds.bind(InetSocketAddress.createUnresolved("192.168.14.89", 8076));
				
			}
			if(dp==null){
	        	   dp =new DatagramPacket(buf,1024); 
			}
	        
	        ds.receive(dp);  

	         String strInfo= new String(dp.getData(),0,dp.getLength())+  " from  "+dp.getAddress().getHostAddress()+":"+dp.getPort(); 
	         System.out.println("----------------"); 

  System.out.println(df.format(new Date())+"received:"+strInfo); 

	         

		 } catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		 }     

		}
		
	}
	
	public static void main(String[] args) {
		new UdpServer().run();
	}
	

}
